package com.tianqu.tms.www.permission.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tianqu.tms.core.util.MenuTagUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.taglib.MenuBean;
import com.tianqu.tms.core.taglib.MenuTag;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.UserFunctionDto;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.permission.viewhelper.PermissionManageViewHelper;

/**
 * @author zhangxueliang
 * 权限修改录入
 */
public class PermissionUpdateInputAction extends TmsAction{
	private Logger logger = Logger.getLogger(PermissionUpdateInputAction.class);
	public static final String CFUNCIDSTR = "cFuncIdStr";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		int role = (Integer) uc.getAttribute("role");
		UserRole userRole = UserRole.getInstance();
		userRole = userRole.findRoleByRole(role);
		PermissionManageViewHelper viewhelper = (PermissionManageViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);

		String funcId = userRole.getFuncIds();
		MenuBean bean = uc.getRootMenuBean();
		List<MenuBean> functionList = new ArrayList<MenuBean>();
		functionList = 	bean.getChildrenList();
		List codesList = new ArrayList();
		String checkedStr = "";
		if(uc.getAttribute(CFUNCIDSTR) != null){
			checkedStr = (String)uc.getAttribute(CFUNCIDSTR);
		}
		
		for(MenuBean menuBean : functionList){
			if(menuBean != null){
				if(menuBean.getLevel() == 1){
					UserFunctionDto dto = new UserFunctionDto();
					dto.setFuncId(menuBean.getFunctionId());
					dto.setFuncName(menuBean.getMenuName());
					dto.setFuncGrade(1);//父
					if(funcId.indexOf(menuBean.getFunctionId()) > -1){
						dto.setChecked(1);
					}else{
						dto.setChecked(2);
					}
					
					codesList.add(dto);
					List<MenuBean> menuBeanCList = menuBean.getChildrenList();
					
					if(menuBeanCList.size() > 0 && menuBeanCList != null){
						int i =1;
						for(MenuBean menuBeanC : menuBeanCList){
							UserFunctionDto dtoC = new UserFunctionDto();
							dtoC.setFuncId(menuBeanC.getFunctionId());
							dtoC.setFuncName(menuBeanC.getMenuName());
							dtoC.setFuncGrade(2);//子
							if(funcId.indexOf(menuBeanC.getFunctionId()) > -1){
								dtoC.setChecked(1);
							}else{
								dtoC.setChecked(2);
							}
						
							if(!StringUtil.isNullOrBlank(checkedStr)){
								if(checkedStr.indexOf(menuBeanC.getFunctionId()) > -1){
									dtoC.setChecked(1);
								}else{
									dtoC.setChecked(2);
								}
							}
							if(i % 2 == 0){
								dtoC.setTrFlag(2);
							}else{
								dtoC.setTrFlag(1);
							}
							if(menuBeanCList.size() == 1){//如果这一行只有一列，那么td要加上colspan="5"，并结束
								dtoC.setTrFlag(3);
							}
							i++;
							codesList.add(dtoC);
						}
					}
				}
			}
		}

		viewhelper.setUserFunctionList(codesList);

		uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		request.setAttribute("viewhelp", viewhelper);
		
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
